package com.fingard.dsp.bank.directbank.tjcb01;

import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.net.ITcpClient;
import com.fingard.net.TcpPortClient;
import com.fingard.net.TcpReceiver;
import com.fingard.net.WebRequest;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextWriter;

public class TJCB01Base extends DirectBase implements ITcpClient {
    protected void writeCommonHeader(XmlTextWriter xmlWriter, TJCB01Header tjcb01Header) throws Exception {
        xmlWriter.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\"" + getCharset() + "\"?>");
        xmlWriter.writeStartElement("transaction");
        xmlWriter.writeStartElement("RequestHeader");
        xmlWriter.writeElementString("transName", tjcb01Header.transName);
        xmlWriter.writeElementString("language", "zh-cn");
        xmlWriter.writeElementString("clientTime", tjcb01Header.clientTime);
        xmlWriter.writeElementString("userID", bankFront.userName);
        xmlWriter.writeElementString("userPassword", bankFront.userPassword);
        xmlWriter.writeElementString("operatorID", bankFront.operator);
        xmlWriter.writeElementString("batchID", tjcb01Header.batchID);
        xmlWriter.writeElementString("transCount", tjcb01Header.transCount);
        xmlWriter.writeEndElement();
    }

    protected String[] sendToBank(String p_reqStr, String transType, String commandCode) throws Exception {
        String[] retStr = new String[]{"", ""};
        if (StringHelper.hasAnyChar(transType) && StringHelper.hasAnyChar(commandCode)) {
            this.detailSimulateForBank(bankFront.bankCode, transType, commandCode, p_reqStr, retStr);
            if (retStr[1].length() > 0) {
                return retStr;
            }
        }
        String tmpUrl = bankFront.serverURL + "?SIGDATA=1&userPassword=" + bankFront.loginPasswd;
        WriteBankLogLn("1.提交银行的地址：" + tmpUrl);
        WriteBankLogStep2(p_reqStr);
        WebRequest tmpWebRequest = new WebRequest(tmpUrl);
        tmpWebRequest.setConnectTimeout(180000);//设置连接超时
        tmpWebRequest.setRequestProperty("Method", "POST");//设置请求属性
        tmpWebRequest.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");//设置请求属性
        tmpWebRequest.setRequestProperty("connection", "close");//设置连接状态
        byte[] tmpUpBytes = p_reqStr.getBytes(getCharset());
        retStr = tmpWebRequest.upload(tmpUpBytes, getCharset());
        WriteBankLogStep3(retStr[1]);
        return retStr;

    }

    protected String getCharset() {
        return getBankConfig().getCharset("GB2312");
    }

    @Override
    public void getContentLength(TcpReceiver p_receiver) throws Exception {
    }
}
